package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.OrgFeignService;
import com.alibaba.algo.cooperation.feign.service.RoleFeignService;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.SelectOrgUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectRoleUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.OrgUserInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.RoleUserInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserEmployeeResVO;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.TaskCompletedPostProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.exception.EventCallSuspendException;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.FlowActivityApproverVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.FlowTaskFinishedNoticeVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.FlowTaskInstApproverVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowActivityAproverRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskFinishedNoticeRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskInstApproverRepository;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: liuhuimin
 * @date:2023/03/22 15:02
 * @description:  记录阅知任务实例信息
 */
@Component
public class RecordTaskInstNoticePostProcessor  implements TaskCompletedPostProcessor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FlowActivityAproverRepository  flowActivityAproverRepository;

    @Autowired
    private FlowTaskInstApproverRepository flowTaskInstApproverRepository;

    @Autowired
    private OrgFeignService orgFeignService;

    @Autowired
    private RoleFeignService  roleFeignService;

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private FlowTaskFinishedNoticeRepository  flowTaskFinishedNoticeRepository;

    @Override
    public void process(TaskEntity taskEntity) throws EventCallSuspendException {
        String taskId = taskEntity.getId();
        //阅知逻辑耗时太长，先取消
//        String  assignee = taskEntity.getAssignee();
//        Task task = SpringUtils.getBean(TaskService.class).createTaskQuery().taskId(taskId).singleResult();
//        String procDefId = task.getProcessDefinitionId();
//        String activityId = task.getTaskDefinitionKey();
//        ProcessDefinition processDefinition = SpringUtils.getBean(RepositoryService.class).createProcessDefinitionQuery().processDefinitionId(procDefId).singleResult();
//        String deployId = processDefinition.getDeploymentId();
//        List<FlowActivityApproverVo> flowActivityApproverVoList =  flowActivityAproverRepository.selectByDeployIdAndActivityId(deployId, activityId);
//        Set<Long> userIds = new HashSet<>();
//        if(flowActivityApproverVoList.size() > 0){
//            for(FlowActivityApproverVo flowActivityApproverVo : flowActivityApproverVoList){
//                String type = flowActivityApproverVo.getType();
//                if("ORG".equals(type)){
//                    Long orgId = Long.parseLong(flowActivityApproverVo.getCode());
//                    SelectOrgUserReqVO  selectOrgUserReqVO = new SelectOrgUserReqVO();
//                    selectOrgUserReqVO.setOrgId(orgId);
//                    BaseResult<List<OrgUserInfoResVO>> result  = orgFeignService.qryUserListByOrgId(selectOrgUserReqVO);
//                    List<OrgUserInfoResVO> userList = result.getData();
//                    if(userList != null){
//                        for(OrgUserInfoResVO orgUserInfoResVO : userList){
//                            Long userId = orgUserInfoResVO.getUserId();
//                            userIds.add(userId);
//                        }
//                    }
//                }else if("ROL".equals(type)){
//                    SelectRoleUserReqVO   selectRoleUserReqVO =  new SelectRoleUserReqVO();
//                    selectRoleUserReqVO.setRoleId(Long.parseLong(flowActivityApproverVo.getCode()));
//                    BaseResult<List<RoleUserInfoResVO>>  result  = roleFeignService.qryUserListByRoleId(selectRoleUserReqVO);
//                    List<RoleUserInfoResVO>  userList =  result.getData();
//                    if(userList != null){
//                        for(RoleUserInfoResVO roleUserInfoResVO : userList){
//                            userIds.add(roleUserInfoResVO.getUserId());
//                        }
//                    }
//                }else if("STA".equals(type)){
//                    userIds.add(Long.parseLong(flowActivityApproverVo.getCode()));
//                }
//            }
//        }else{
//            List<FlowTaskInstApproverVo> flowTaskInstApproverVoList =    flowTaskInstApproverRepository.selectListByTaskId(taskId);
//            for(FlowTaskInstApproverVo flowTaskInstApproverVo : flowTaskInstApproverVoList){
//                userIds.add(Long.parseLong(flowTaskInstApproverVo.getUserId()));
//            }
//        }
//        this.saveNoticeInfo(userIds, assignee, taskId);
    }

    private   void  saveNoticeInfo(Set<Long> userIds, String assignee, String taskId){
        Long assigneeId =  Long.parseLong(assignee);
        for(Long userId : userIds){
            if(!userId.equals(assigneeId)){
                SelectUserInfoReqVO  selectUserInfoReqVO = new  SelectUserInfoReqVO();
                selectUserInfoReqVO.setUserId(userId);
                BaseResult<RpcUserInfoVO>  result  =  userFeignService.qryUserInfo(selectUserInfoReqVO);
                RpcUserInfoVO rpcUserInfoVO = result.getData();
                if(rpcUserInfoVO != null){
                    String userName = rpcUserInfoVO.getUserName();
                    UserEmployeeResVO userEmployeeResVO = rpcUserInfoVO.getEmployeeInfo();
                    String  employeeCode = null;
                    if(userEmployeeResVO != null){
                        employeeCode =  userEmployeeResVO.getEmployeeCode();
                    }
                    FlowTaskFinishedNoticeVo flowTaskFinishedNoticeVo = new  FlowTaskFinishedNoticeVo();
                    flowTaskFinishedNoticeVo.setTaskId(taskId);
                    flowTaskFinishedNoticeVo.setUserId(userId);
                    flowTaskFinishedNoticeVo.setUserName(userName);
                    flowTaskFinishedNoticeVo.setEmployeeCode(employeeCode);
                    flowTaskFinishedNoticeVo.setCreateTime(new Date());
                    String userAccount = rpcUserInfoVO.getUserAccount();
                    flowTaskFinishedNoticeVo.setUserAccount(userAccount);
                    flowTaskFinishedNoticeRepository.insert(flowTaskFinishedNoticeVo);
                }
            }
        }
    }

    @Override
    public String getDesc() {
        return null;
    }
}

